package com.smartlib.cmnObject.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.JsonNull;

/**
 * Created by admin on 16/6/23.
 * sp util
 */
public class SPUtil {
    private SharedPreferences mSp = null;

    private static String tag = "project";

    private static SPUtil mSPUtil = null;

    private SPUtil(Context context) {
        mSp = context.getSharedPreferences(tag, Context.MODE_PRIVATE);
    }

    public static synchronized SPUtil getInstance(Context context) {
        if (mSPUtil == null) {
            mSPUtil = new SPUtil(context);
        }
        return mSPUtil;
    }


    /***
     * put String
     *
     * @param key
     * @param value
     */
    public void putString(String key, String value) {
        if (mSp != null) {
            SharedPreferences.Editor editor = mSp.edit();
            editor.putString(key, value);
            editor.apply();
        }
    }

    /***
     * get String from sp
     *
     * @param key
     */
    public String getString(String key) {
        return getString(key, "");
    }

    /***
     * get String from sp
     *
     * @param key
     * @param defValue 默认值
     * @return
     */
    public String getString(String key, String defValue) {
        String value = "";
        if (mSp != null) {
            value = mSp.getString(key, defValue);
        }

        return value;
    }

    /***
     * get int from sp
     *
     * @param key
     * @return
     */
    public int getInt(String key, int defValue) {
        int value = 0;
        if (mSp != null) {
            value = mSp.getInt(key, defValue);
        }
        return value;

    }

    public int getInt(String key) {
        return getInt(key, 0);
    }

    /***
     * put int to sp
     *
     * @param key
     * @param value
     */
    public void putInt(String key, int value) {
        if (mSp != null) {
            SharedPreferences.Editor editor = mSp.edit();
            editor.putInt(key, value);
            editor.apply();
        }
    }

    /***
     * put float
     *
     * @param key
     * @param value
     */
    public void putfloat(String key, float value) {
        if (mSp != null) {
            SharedPreferences.Editor editor = mSp.edit();
            editor.putFloat(key, value);
            editor.apply();
        }
    }

    /***
     * get object from sp
     *
     * @param key
     * @param tClass
     * @param <T>
     * @return
     */
    public <T> T getObject(String key, Class<T> tClass) {
        T t = null;
        String value = getString(key);
        if (StringUtil.isJson(value)) {
            t = GsonUtil.praseFromStringToBean(value, tClass);
        }
        return t;
    }

    /**
     * put object to sp
     *
     * @param key
     * @param object
     */
    public void putObject(String key, Object object) {
        String value = GsonUtil.parseFromBeanToString(object);
        putString(key, value);
    }

    /***
     * get float
     *
     * @param key
     * @return
     */
    public float getFloat(Context context, String key) {
        return getFloat(context, key, 0.0f);
    }

    /***
     * get float
     *
     * @param key
     * @param defValue
     * @return
     */
    public float getFloat(Context context, String key, float defValue) {
        float value = 0.0f;
        if (mSp != null) {
            value = mSp.getFloat(key, defValue);
        }

        return value;
    }

    public boolean contain(Context context, String key) {
        boolean value = false;
        if (mSp != null) {
            value = mSp.contains(key);
        }

        return value;
    }

    public boolean getBoolean(Context context,String key)
    {
        boolean value = false;
        if(mSp!=null)
        {
            value = mSp.getBoolean(key,false);
        }
        return value;
    }

    public void putBoolean(Context context,String key,boolean value)
    {
        if(mSp!=null)
        {
            mSp.edit().putBoolean(key,value).commit();
        }
    }

}
